// components/scroll-video.js
Component({
  options: {
    multipleSlots: true // 在组件定义时的选项中启用多slot支持
  },
  /**
   * 组件的属性列表
   */
  properties: {
    //父组件传入的视频列表
    videoList:{
      type: Array,
      value: [],
      observer: function(newVal, oldVal){
        this.setData({
          videoSize: newVal.length
        });
      }
    },
    //视频或者直播适配页面方式
    fitType:{
      type: String,
      value: 'cover',
    },
    store_setting: {
      type: null,
      value: '',
    },
    //滑动距离的设置 超过该距离回出现页面下滑或者上滑的情况
    thresholdValue:{
      type: Number,
      value: 100,
      observer: function (newVal, oldVal) {
        console.log(newVal,oldVal);
      }
    },
    //播放器类型
    playerType:{
      type: String,
      value: 'video',
      observer: function (newVal, oldVal) {
      }
    }
    
  },

  /**
   * 组件的初始数据
   */
  data: {
    startY: 0,//开始y点
    screenHeight:0,//获取当前屏幕高度
    screenWidth:0,
    scrollAnimate:0,
    videoidx:0,//保存切换下标
    videoSize:0,//视频列表的长度
    palyVideo:null,
    showgoods: 0,
    vgoods: null
  },
  ready:function(){
    var that = this;
    that.data.palyVideo = wx.createVideoContext('vide_index_0',that);
    that.data.palyVideo.play()
    setTimeout(function(){
      that.setData({
        vgoods:that.properties.videoList[0]
      })
    },1000)
  },
  /**
   * 组件的方法列表
   */
  methods: {
    buttonhandle:function(e){
      const { buttontype, buttonname, itemid } = e.detail;
      this.triggerEvent('menuTap', { buttontype, buttonname, itemid });
    },
    updateIndex: function (e) {
      var videoidx = e.detail.current;
      var that = this;
      this.properties.videoList.forEach((item,i)=>{
        if (videoidx === i){
          if (that.data.palyVideo) {
            that.data.palyVideo.pause()
          }
          setTimeout(function() {
            that.data.palyVideo = wx.createVideoContext('vide_index_' + i,that);
            that.data.palyVideo.play()
          },400)
        }
      });
      var vgoods = that.properties.videoList[videoidx];
      var good_vgoods = wx.getStorageSync('good_vgoods');
      if (good_vgoods && good_vgoods.indexOf(vgoods.id) > -1) {
        vgoods.good = 1
      } else {
        vgoods.good = 0
      }
      this.setData({
        vgoods: vgoods
      })
      this.triggerEvent('swipeUpper', {
        index: videoidx
      });
    },
    show_user: function () {
      this.triggerEvent('show_user');
    },
    showgoods:function(){
      this.setData({
        showgoods:true
      })
    },
    hidegoods:function(){
      this.setData({
        showgoods:false
      })
    },
    

    jia: function(t) {
      if (!wx.getStorageSync('USER_INFO') || !wx.getStorageSync('USER_INFO').id >0) {
        this.triggerEvent('show_user');
        // this.setData({
        //   user_info_show:!0
        // })
        return false;
      }
      var that = this,
        o = t.currentTarget.dataset,
        r = that.properties.vgoods.goods_list;
  
      var goods_id = t.currentTarget.dataset.id;
      for (var s in r)
        if (parseInt(r[s].id) === parseInt(goods_id)) {
          if (r[s].id == goods_id) {
            wx.showLoading({
              title: "加载中"
            })
            var attr = JSON.parse(r[s].attr)[0].attr_list[0];
            getApp().request({
              url: getApp().api.cart.add_cart,
              method: "POST",
              data: {
                goods_id: parseInt(r[s].id),
                attr: '[{"attr_group_id":1,"attr_group_name":"规格","attr_id":' + attr.attr_id + ',"attr_name":"' + attr.attr_name + '"}]',
                num: 1
              },
              success: function(t) {
                wx.hideLoading();
                if (t.code == 0) {
                  wx.showToast({
                    icon: 'none',
                    title: t.msg,
                  })
                } else {
                  wx.showToast({
                    icon: 'none',
                    title: t.msg,
                    duration: 1500
                  })
                }
                // that.cart_num();
              },
              complete: function(res) {
                wx.hideLoading()
              }
            })
          }
        }
    },
    showDialogBtn: function (o) {
      if (!wx.getStorageSync(getApp().const.USER_INFO) || wx.getStorageSync(getApp().const.USER_INFO).length == 0) {
        this.setData({
          user_info_show: !0
        })
        return false;
      }
      var r = this,
        i = o.currentTarget.dataset,
        goods_id = o.currentTarget.dataset.id;
      wx.showLoading({
        title: "加载中"
      })
      getApp().request({
        url: getApp().api.default.goods,
        data: {
          id: goods_id
        },
        success: function (t) {
          wx.hideLoading()
          0 == t.code && (r.setData({
            currentGood: t.data,
            goods_name: t.data.name,
            attr_group_list: t.data.attr_group_list,
            showModal: !0
          }), r.resetData(), r.updateData());
        },
        complete: function () {
          wx.hideLoading()
        }
      });
    },
    resetData: function () {
      this.setData({
        checked_attr: [],
        check_num: 0,
        check_goods_price: 0,
        temporaryGood: {
          price: "0.00"
        }
      });
    },
    updateData: function () {
      var t = this,
        a = t.data.currentGood,
        o = t.data.carGoods,
        r = JSON.parse(a.attr),
        i = a.attr_group_list;
      for (var s in r) {
        var e = [];
        for (var d in r[s].attr_list) e.push([r[s].attr_list[d].attr_id, a.id]);
        for (var c in o) {
          var n = [];
          for (var u in o[c].attr) n.push([o[c].attr[u].attr_id, o[c].goods_id]);
          if (e.sort().join() === n.sort().join()) {
            for (var _ in i)
              for (var g in i[_].attr_list)
                for (var l in e) {
                  if (parseInt(i[_].attr_list[g].attr_id) === parseInt(e[l])) {
                    i[_].attr_list[g].checked = !0;
                    break;
                  }
                  i[_].attr_list[g].checked = !1;
                }
            var h = {
              price: o[c].price
            };
            return void t.setData({
              attr_group_list: i,
              check_num: o[c].num,
              check_goods_price: o[c].goods_price,
              checked_attr: e,
              temporaryGood: h
            });
          }
        }
      }
    },
    attrClick: function(t) {
      var a = this,
        o = t.target.dataset.groupId,
        r = t.target.dataset.id,
        i = a.data.attr_group_list,
        s = a.data.currentGood;
      for (var e in i)
        if (i[e].attr_group_id == o)
          for (var d in i[e].attr_list) i[e].attr_list[d].attr_id == r ? i[e].attr_list[d].checked = !0 : i[e].attr_list[d].checked = !1;
      var c = [];
      for (var e in i)
        for (var d in i[e].attr_list) !0 === i[e].attr_list[d].checked && c.push([i[e].attr_list[d].attr_id, s.id]);
      var n = JSON.parse(a.data.currentGood.attr),
        u = a.data.temporaryGood;
      for (var _ in n) {
        var g = [];
        for (var l in n[_].attr_list) g.push([n[_].attr_list[l].attr_id, s.id]);
        if (g.sort().join() === c.sort().join()) {
          if (0 === parseInt(n[_].num)) return void wx.showToast({
            title: "商品库存不足",
            image: "/images/icon-warning.png"
          });
          u = parseFloat(n[_].price) ? {
            price: n[_].price.toFixed(2)
          } : {
            price: s.price.toFixed(2)
          };
        }
      }
      a.resetData(), a.checkUpdateData(c), a.setData({
        attr_group_list: i,
        temporaryGood: u,
        checked_attr: c
      });
    },
    resetData: function() {
      this.setData({
        checked_attr: [],
        check_num: 0,
        check_goods_price: 0,
        temporaryGood: {
          price: "0.00"
        }
      });
    },
    checkUpdateData: function(t) {
      var a = this,
        o = a.data.carGoods;
      for (var r in o) {
        var i = [];
        for (var s in o[r].attr) i.push([o[r].attr[s].attr_id, o[r].goods_id]);
        i.sort().join() === t.sort().join() && a.setData({
          check_num: o[r].num,
          check_goods_price: o[r].goods_price
        });
      }
    },
    onConfirm: function(t) {
      var a = this,
        o = a.data.attr_group_list,
        r = a.data.checked_attr,
        i = a.data.currentGood;
      if (r.length === o.length) {
        var s = a.data.check_num ? a.data.check_num + 1 : 1,
          e = JSON.parse(i.attr);
        for (var d in e) {
          var c = [];
          for (var n in e[d].attr_list)
            if (c.push([e[d].attr_list[n].attr_id, i.id]), c.sort().join() === r.sort().join()) {
              var u = e[d].price ? e[d].price : i.price,
                _ = e[d].attr_list;
              if (s > e[d].num) return void wx.showToast({
                title: "商品库存不足",
                image: "/images/icon-warning.png"
              });
            }
        }
  
        for (var ii in o) {
          for (var kk in o[ii].attr_list) {
            if (o[ii].attr_list[kk].checked) {
              o[ii].attr_id = o[ii].attr_list[kk].attr_id;
              o[ii].attr_name = o[ii].attr_list[kk].attr_name;
            }
          }
          delete o[ii].attr_list;
        }
        wx.showLoading({
          title: '加载中',
        })
        getApp().request({
          url: getApp().api.cart.add_cart,
          method: "POST",
          data: {
            goods_id: i.id,
            attr: JSON.stringify(o),
            num: 1
          },
          success: function(t) {
            if (t.code == 0) {
              wx.showToast({
                title: t.msg,
                duration: 1500
              });
            }
          },
          complete: function() {
            wx.hideLoading()
          }
        })
  
        var g = a.data.carGoods,
          l = 1,
          h = (parseFloat(u) * s).toFixed(2);
        for (var p in g) {
          var f = [];
          for (var v in g[p].attr) f.push([g[p].attr[v].attr_id, g[p].goods_id]);
          if (f.sort().join() === r.sort().join()) {
            l = 0, g[p].num = g[p].num + 1, g[p].goods_price = (parseFloat(u) * g[p].num).toFixed(2);
            break;
          }
        }
        a.hideGoodsModel();
      } else wx.showToast({
        title: "请选择规格",
        image: "/images/icon-warning.png"
      });
    },
    hideGoodsModel: function() {
      this.setData({
        showModal: !1
      });
    },
    updateData: function() {
      var t = this,
        a = t.data.currentGood,
        o = t.data.carGoods,
        r = JSON.parse(a.attr),
        i = a.attr_group_list;
      for (var s in r) {
        var e = [];
        for (var d in r[s].attr_list) e.push([r[s].attr_list[d].attr_id, a.id]);
        for (var c in o) {
          var n = [];
          for (var u in o[c].attr) n.push([o[c].attr[u].attr_id, o[c].goods_id]);
          if (e.sort().join() === n.sort().join()) {
            for (var _ in i)
              for (var g in i[_].attr_list)
                for (var l in e) {
                  if (parseInt(i[_].attr_list[g].attr_id) === parseInt(e[l])) {
                    i[_].attr_list[g].checked = !0;
                    break;
                  }
                  i[_].attr_list[g].checked = !1;
                }
            var h = {
              price: o[c].price
            };
            return void t.setData({
              attr_group_list: i,
              check_num: o[c].num,
              check_goods_price: o[c].goods_price,
              checked_attr: e,
              temporaryGood: h
            });
          }
        }
      }
    },
    get_goods_info: function(t) {
      var d = t.currentTarget.dataset.id;
      wx.navigateTo({
        url: "/pages/goods/goods?id=" + d
      });
    },
    hideModal: function() {
      this.setData({
          showModal: !1
      });
    },
  }
})
